package mi.payment.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import sunmi.sunmiui.utils.LogUtil;

/**
 * 判断网络状态工具类
 *
 * @author longtao.li
 */
public class NetUtils {

    /**
     * 判断当前网络是否是wifi网络
     */
    public static boolean isWifi(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetInfo != null
                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
    }

    /**
     * 判断当前网络是否是3G网络
     */
    public static boolean is3G(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    }

    /**
     * 判断是否有网络链接
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager
                    .getActiveNetworkInfo();
            if (networkInfo != null) {
                return networkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 检查外网是否联通
     */
    public static boolean isExternalNetConnected() {
        try {
            final Runtime runtime = Runtime.getRuntime();
            int exitValue1;
            //使用公共域名解析服务DNS:114.114.114.114
            //执行Ping命令 格式为  ping -c 1 -w 2
            //其中参数-c 1是指ping的次数为1次，-w是指执行的最后期限,单位为秒，也就是执行的时间为2秒，超过2秒则失败.
            Process ipProcess = runtime.exec("ping -c 2 -w 2 114.114.114.114");
            exitValue1 = ipProcess.waitFor();
            InputStream input = ipProcess.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = in.readLine()) != null) {
                buffer.append("   " + line);
            }
            LogUtil.e("ping  Return ============", buffer.toString());
            return (exitValue1 == 0);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 检查网络是否可用
     */
    public static boolean isNetworkUsable(Context context) {
        if (!isNetworkConnected(context)) {
            return false;
        }
//        if (isWifi(context) && !isExternalNetConnected()) {
//            return false;
//        }
        return true;
    }

}
